package com.example.auth.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ResultUtil {

    /**
     * 私有化构造器
     */
    private ResultUtil(){}

    /**
     * 使用response输出JSON
     * @Author Sans
     * @CreateTime 2019/9/28 11:23
     * @Param  resultMap 数据
     * @Return void
     */
    public static void responseJson(ServletResponse response, Map<String, Object> resultMap){
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.println(JSON.toJSONString(resultMap));
        } catch (Exception e) {
            log.error("【JSON输出异常】"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }
    /**
     * 返回成功示例
     * @Author Sans
     * @CreateTime 2019/9/28 11:29
     * @Param  resultMap  返回数据MAP
     * @Return Map<String,Object> 返回数据MAP
     */
    public static Map<String, Object> resultSuccess(Map<String, Object> resultMap){
        resultMap.put("message","操作成功");
        resultMap.put("code", 200);
        return resultMap;
    }
    /**
     * 返回失败示例
     * @Author Sans
     * @CreateTime 2019/9/28 11:31
     * @Param  resultMap  返回数据MAP
     * @Return Map<String,Object> 返回数据MAP
     */
    public static Map<String, Object> resultError(Map<String, Object> resultMap){
        resultMap.put("message","操作失败");
        resultMap.put("code",500);
        return resultMap;
    }
    /**
     * 通用示例
     * @Author Sans
     * @CreateTime 2019/9/28 11:35
     * @Param  code 信息码
     * @Param  msg  信息
     * @Return Map<String,Object> 返回数据MAP
     */
    public static Map<String, Object> resultCode(Integer code,String msg){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("message",msg);
        resultMap.put("code",code);
        return resultMap;
    }

    /**
     * 通用JSON返回示例
     * @param code
     * @param msg
     * @return
     */
    public static JSONObject resultCodeJSON(Integer code,String msg){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        return jsonObject;
    }

    /**
     * 通用返回
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static JSONObject resultObjectJSON(Integer code,String msg,Object data){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        jsonObject.put("data",data);
        return jsonObject;
    }

    public static JSONObject errorInfo(){
        JSONObject jsonObject=getJSONObject();
        jsonObject.put("code",1);
        jsonObject.put("message","error");
        return jsonObject;
    }

    public static JSONObject errorInfo(String msg){
        JSONObject jsonObject=errorInfo();
        jsonObject.put("message",msg);
        return jsonObject;
    }

    public static JSONObject msg(Integer code,String msg){
        JSONObject jsonObject=successInfo();
        jsonObject.put("code",code);
        jsonObject.put("message",msg);
        return jsonObject;
    }


    public static JSONObject successInfo(String msg,Object data){
        JSONObject jsonObject=successInfo(msg);
        jsonObject.put("data",data);
        return jsonObject;
    }

    public static JSONObject successInfoTable(Integer count,Object data){
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse resp=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        log.info(req.getRequestURI());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        jsonObject.put("count",count);
        jsonObject.put("data",data);
        return jsonObject;
    }

    public static JSONObject successInfoTable(Long count,Object data){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        jsonObject.put("count",count);
        jsonObject.put("data",data);
        return jsonObject;
    }

    public static JSONObject successInfo(String msg){
        JSONObject jsonObject=successInfo();
        jsonObject.put("message",msg);
        return jsonObject;
    }

    public static JSONObject successInfo(Object data){
        return successInfo("success", data);
    }



    public static JSONObject successInfo(){
        return getJSONObject();
    }

    private static  JSONObject  getJSONObject(){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("message","success");
        return jsonObject;
    }
}
